﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Logging
{
    class LoggerConfig
    {
        private LoggerConfig()
        {
        }

        private string AppConfigPath
        {
            get
            {
                return System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            }
        }

        private void Init(string appenderName)
        {
            m_xmlDoc = new XmlDocument();
            m_xmlDoc.Load(AppConfigPath);
            string xPath = String.Format("configuration/logger/appenders/{0}", appenderName);
            m_element = m_xmlDoc.SelectSingleNode(xPath) as XmlElement;
        }

        public string GetParameter(string name)
        {
            XmlNodeList nodes = m_element.GetElementsByTagName(name);
            if (nodes.Count != 1)
            {
                throw new Exception("Incorrect config file!");
            }
            XmlElement param = nodes[0] as XmlElement;
            return param.GetAttribute("value");
        }

        public static LoggerConfig GetInstance(string appenderName)
        {
            LoggerConfig config = new LoggerConfig();
            config.Init(appenderName);
            return config;               
        }


        XmlElement m_element;
        XmlDocument m_xmlDoc;
    }
}
